package cn.tqfeiyang.springplus.system.controller;

import cn.tqfeiyang.springplus.system.service.UserService;
import cn.tqfeiyang.springplus.system.request.UserDTO;
import cn.tqfeiyang.springplus.system.response.UserVO;
import cn.tqfeiyang.springplus.system.request.UserSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 用户前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/system/user")
@Tag(name = "系统/用户")
public class UserController {

    /**
     * 用户服务
     */
    @Resource
    private UserService userService;

    /**
     * 查询用户
     *
     * @param userSTO 查询数据
     * @return 用户列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询用户")
    @PreAuthorize("hasAnyAuthority('system:user:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<UserVO>> selectUserPage(UserSTO userSTO) {
        return ApiResult.success(userService.selectUserPage(userSTO));
    }

    /**
     * 读取用户
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取用户")
    @PreAuthorize("hasAnyAuthority('system:user:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<UserVO> selectUserInfo(@RequestParam @EntityId Long userId) {
        return ApiResult.success(userService.selectUserInfo(userId));
    }

    /**
     * 新建用户
     *
     * @param userDTO 请求数据
     * @return 用户信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建用户")
    @PreAuthorize("hasAnyAuthority('system:user:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<UserVO> createUser(@RequestBody @Validated(WhenCreate.class) UserDTO userDTO) {
        Long userId = userService.createUser(userDTO);
        return ApiResult.success(userService.selectUserInfo(userId));
    }

    /**
     * 修改用户
     *
     * @param userId 用户id
     * @param userDTO 请求数据
     * @return 用户信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改用户")
    @PreAuthorize("hasAnyAuthority('system:user:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<UserVO> updateUser(@RequestParam @EntityId Long userId, @RequestBody @Validated(WhenUpdate.class) UserDTO userDTO) {
        userService.updateUser(userId, userDTO);
        return ApiResult.success(userService.selectUserInfo(userId));
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除用户")
    @PreAuthorize("hasAnyAuthority('system:user:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteUser(@RequestParam @EntityId Long userId) {
        userService.deleteUser(userId);
        return ApiResult.success();
    }

    /**
     * 查询角色
     *
     * @param userId 用户id
     * @return 角色列表
     */
    @GetMapping("/roles")
    @Operation(summary = "查询角色")
    @PreAuthorize("hasAnyAuthority('system:user:roles')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult selectRoleList(@RequestParam @EntityId Long userId) {
        return ApiResult.success(userService.selectRoleList(userId));
    }
}
